/**
 * X-Report(2012)
 */
package net.abbp.report.designer.applet;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.HeadlessException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import net.abbp.report.designer.IDesigner;
import net.abbp.report.designer.components.resume.ResumeTemplateDesignWorkbench;
import net.abbp.report.designer.components.resume.ResumeTemplateList;
import net.abbp.report.designer.components.resume.panel.NewResumeTemplatePanel;
import net.abbp.report.designer.context.DesignerContext;
import net.abbp.report.designer.toolbar.CommonToolBar;

import com.reportgear.core.swing.pane.base.JBaseDialog;

/**
 * 简历设计器
 * 
 * @version 1.0 2011-3-14
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 * 
 */
public class ResumeDesigner extends JPanel implements IDesigner {
    /**
     * UID
     */
    private static final long serialVersionUID = 6283908415171616535L;
    private ResumeTemplateList reportList;
    private ResumeTemplateDesignWorkbench workbench;

     private JSplitPane mainPane;

    public ResumeDesigner() throws HeadlessException {
        this.initComponents();
        this.initDesigner();
    }

    /**
     * 初始化控件
     */
    private void initComponents() {
        this.setLayout(new BorderLayout());
        JToolBar styleBar = new CommonToolBar();
        this.add(styleBar, BorderLayout.NORTH);

        this.reportList = new ResumeTemplateList();
        this.workbench = new ResumeTemplateDesignWorkbench();
       
        this.reportList.addPropertyChangeListener("CurrModel", this.workbench);
        this.reportList.selectFirst();

        mainPane = this.createSplitPane(this.reportList, new JScrollPane(this.workbench));

         this.add(this.mainPane, BorderLayout.CENTER);

    }

    /**
     * 建立分隔面板
     * 
     * @param left
     *            左面板
     * @param right
     *            右面板
     * @return 分隔面板
     */
    private JSplitPane createSplitPane(Component left, Component right) {
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        pane.setOneTouchExpandable(true);
        pane.setDividerLocation(200);

        return pane;
    }

    /**
     * 初始化编辑器
     */
    private void initDesigner() {
        DesignerContext.INSTANCE.setDesigner(this);
    }

    /**
     * 创建新报表
     */
    public void newModel() {
        NewResumeTemplatePanel pane = new NewResumeTemplatePanel();
        int result = pane.showDialog(SwingUtilities.getWindowAncestor(this), "新建简历报表", 400, 140);

        if (result == JBaseDialog.RESULT_OK) {
            if (pane.isInputValid()) {
                this.reportList.newReport(pane.getName());
            } else {
                JOptionPane.showMessageDialog(this, "输入信息不完整!");
            }
        }

    }

    /**
     * 保存报表
     */
    public void saveModel() {
        this.workbench.saveReport();
    }

    /**
     * 删除报表
     */
    public void deleteModel() {
        if (JOptionPane.showConfirmDialog(this, "请确认删除报表模板", "删除", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            this.reportList.deleteReport();
        }
    }
}
